-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added .env support #2164
Added .env support #2164
Conversation
@@ -120,6 +120,47 @@ def find_default_import_path(): | |||
return app | |||
|
|||
|
|||
def find_dotenv(): | |||
here = os.getcwd() | |||
while 1: |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
if not line or line[:1] == '#' or '=' not in line: | ||
continue | ||
key, value = line.split('=', 1) | ||
_set_env(key, value) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
if not line or line[:1] == '#' or '=' not in line: | ||
continue | ||
key, value = line.split('=', 1) | ||
_set_env(key, value) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
with open(path, 'rb') as f: | ||
for line in f: | ||
line = line.strip() | ||
if not line or line[:1] == '#' or '=' not in line: |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM though I really want tests for this.
with open(path, 'rb') as f: | ||
for line in f: | ||
line = line.strip() | ||
if not line or line[:1] == '#' or '=' not in line: |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Yeah, tests will come if we like this behavior. |
In case it gets lost: #2164 (comment) |
isnt there a library for env files? imho flask shouldnt reimplement |
@RonnyPfannschmidt i'm not going to pull in a one liner dependency here. @untitaker the behavior here is consistent with other libraries that implement .env loading. |
|
||
FLASK_APP=/path/to/hello.py | ||
FLASK_DEBUG=1 | ||
|
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
So maybe we put the conversation back to where this started: the goal was to make it easier during development to refer to the current Flask app. I do not see this as a feature for production. However let's see if we can figure out the production case as well. How does one refer to their own app in production without exporting env variables? |
@mitsuhiko I agree, good idea to pull back a bit. I think referring to the app instance should work in the same way for development and production, I don't see why the methods should differ, since the app instance is the same in all cases. And there is also the complication of app factories, which are not friendly to What I do, is define a |
I've also commonly seen |
@@ -11,6 +11,9 @@ Major release, unreleased | |||
- Make `app.run()` into a noop if a Flask application is run from the | |||
development server on the command line. This avoids some behavior that | |||
was confusing to debug for newcomers. | |||
- The `flask` command line tool now loads a `.env` file when it discovers | |||
one. When it finds one it will change into that folder before starting |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
I do it by not using the cli = FlaskGroup(create_app=create_app) setup(
...,
entry_points={
'console_scripts': [
'myapp=myapp.cli:cli.main',
],
},
...,
)
|
One option for making this play nicer with the factory pattern is to allow making a call in the |
Is this a valid question? Because you could also say "how does one refer to their own app in production without creating an instance/settings.py file", and then it becomes the same thing. At the end of the day, this file needs to be on the production box for the app to be usable. Personally, I use Docker and it has mechanisms for supplying an Without Docker, I would likely drop an environment file in In other words, in both development and production this env file would be available to the Flask app. If this feature does end up being implemented, please let us configure the I would much prefer to use env variables instead of config/instance files because it plays much nicer when trying to scale applications on infrastructures that let you define clusters of servers, but then can define env variables in a central area (outside of each individual host), and then they become available to all hosts without worrying about the logistics of getting an env file on the system. This could be done now of course, but it kind of goes against the norm which is to use config/instance based config files instead of env variables. |
If I'm understanding this correctly, this would only read the If this was going to be added, I'd rather see it implemented into either the Flask class or config class to at least be consistent between server options. |
I did it by adding this into virtual environment's For example:
Windows CMD.exe:
|
I don't recommend editing |
@untitaker More detail? |
I already elaborated in the PR you sent. You are editing random files which will get overwritten if you recreate the virtualenv (e.g. because it grew too big, or because a Python upgrade trashed it). |
How about adding an entry point in setup.py to tell the flask command where the app is, similar to how we can add extra commands? Since we're encouraging the use of setup.py anyway, might as well take advantage. Not sure how we would handle multiple packages with the entry point though, if that ever comes up. |
See #2234 (comment) |
I'm leaning on the side of rejecting this. I agree that it would cause confusion, and we already have a documented way to set the env when working locally. |
Hmm, I'm not sure if we have discussed this enough yet. I only merged #2234
because it enhanced the docs for existing recommendations.
I would actually prefer a file separate from the virtualenv activate script,
for the additional reason that you would have to edit it for multiple shells.
…On Fri, Apr 07, 2017 at 09:39:15AM -0700, David Lord wrote:
Closed #2164.
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#2164 (comment)
|
This will instruct Flask to read .env files and then to load the
environment variables from there. Additionally it automatically
changes the working directory to the path the .env is contained in.